﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLBV.Models;
using System.Text;

namespace QLBV.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Header()
        {
            return this.PartialView("Header");
        }
        public ActionResult Menu_Top()
        {
            ViewBag.string_menu_top = new MvcHtmlString(displayMenu(0, 1).ToString());

            return this.PartialView("Menu_Top");
        }
        public IList<Menus> Get_Menu(int id, int vitri)
        {
            IList<Menus> menu;
            using (var db = new QLBVContext())
            {
                menu = (from b in db.Menus where b.Parent_ID == id && b.Location == vitri && b.Status == true select b).ToList();
            }
            return menu;
        }
        public ActionResult Tin_Doc_Nhieu()
        {
            return this.PartialView("Tin_Doc_Nhieu");
        }
        public ActionResult Dang_Nhap()
        {
            return this.PartialView("Dang_Nhap");
        }
        public ActionResult Menu_Left()
        {
            ViewBag.string_menu_left = new MvcHtmlString(displayMenu(0, 2).ToString());
            return this.PartialView("Menu_Left");
        }
        public ActionResult Quang_Cao()
        {
            return this.PartialView("Quang_Cao");
        }
        public ActionResult Footer()
        {
            return this.PartialView("Footer");
        }
        public ActionResult Avatar(int id)
        {
            var db = new QLBVContext();
            var nd = db.Employees.Where(nv => nv.ID == id).FirstOrDefault();
            return this.PartialView("Avatar", nd);
        }


        public StringBuilder displayMenu(int p, int vitri)
        {
            StringBuilder strMenu = new StringBuilder();

            IList<Menus> menu;
            menu = Get_Menu(p, vitri);
            if (menu.Count > 0)
            {
                if (p == 0)
                {
                    strMenu.Append("<ul class='sf-menu'>");
                }
                else
                {
                    strMenu.Append("<ul>");
                }

                foreach (var item in menu)
                {
                    strMenu.AppendFormat("<li><a href='{0}'>{1}</a>", item.Link.ToString(), item.Name.ToString());
                    strMenu.Append(displayMenu((int)item.ID, vitri));
                }
                if (p == 0)
                {
                    strMenu.Append("</li></ul>");
                }
                else
                {
                    strMenu.Append("</ul>");
                }

            }
            else
            {
                strMenu.Append("</li>");
            }
            return strMenu;
        }
    }
}
